package stock

import (
	"bufio"
	"io"
	"net/http"
	"os"
	"strings"
	"time"
)

type stock struct{

}

func (s *stock)init() *stock {
	return s
}

func StockInit() *stock{
	return 	new(stock).init()
}



func (s *stock)ReadExcel(fileName string,sheet string) ([][]string,error) {
	f, err := os.Open(fileName)
	if err != nil {
		return nil,err
	}else{
		defer f.Close()
		scanner := bufio.NewScanner(f)
		result := [][]string{}
		for scanner.Scan() {
			lineText := scanner.Text()
			line := strings.Fields(lineText)
			result = append(result,line)
		}
		return result,nil
	}
}

func (s *stock) DownloadExcel(fileName string,url string) error {
	client := http.DefaultClient
	client.Timeout = time.Second * 60 //设置超时时间
	resp, err := client.Get(url)
	defer resp.Body.Close()
	if err != nil {
		return err
	}else{
		if file, err := os.Create(fileName); err != nil {
			return err
		} else{
			defer file.Close()
			_, err = io.Copy(file, resp.Body)
			return err
		}
	}
}